/*
 * @Author: szx
 * @Date: 2021-10-14 19:03:37
 * @LastEditTime: 2021-10-14 19:48:54
 * @Description:
 * @FilePath: \leetcode\852\825.go
 */

package main

import (
	"fmt"
)

// func peakIndexInMountainArray(arr []int) int {
// 	peak := arr[0]
// 	i := 1
// 	for ; i < len(arr); i++ {
// 		if peak > arr[i] {
// 			break
// 		} else {
// 			peak = arr[i]
// 		}
// 	}
// 	i--
// 	return i
// }

func peakIndexInMountainArray(arr []int) int {
	for i := 1; ; i++ {
		if arr[i] > arr[i+1] {
			return i
		}
	}
}

// func peakIndexInMountainArray(arr []int) int {
// 	return sort.Search(len(arr)-1, func(k int) bool { return arr[k+1] < arr[k] })
// }

func Search(n int, f func(int) bool) int {
	// Define f(-1) == false and f(n) == true.
	// Invariant: f(i-1) == false, f(j) == true.
	i, j := 1, n
	for i < j {
		h := int(uint(i+j) >> 1) // avoid overflow when computing h
		// i ≤ h < j
		if !f(h) {
			i = h + 1 // preserves f(i-1) == false
		} else {
			j = h // preserves f(j) == true
		}
	}
	// i == j, f(i-1) == false, and f(j) (= f(i)) == true  =>  answer is i.
	return i
}

func main() {
	arr := []int{24, 69, 100, 99, 79, 78, 67, 36, 26, 19}
	fmt.Println(peakIndexInMountainArray(arr))
}
